import { Component, Injector, ViewEncapsulation, ViewChild, OnInit, EventEmitter, Output } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ModalDirective } from 'ngx-bootstrap';
import { MaterialOutServiceProxy, MaterialChargeListDto } from '@shared/service-proxies/service-proxies';
import { finalize } from 'rxjs/operators';

@Component({
  selector: 'yl2OutModal',
  templateUrl: './yl2-out-modal.component.html',
  //styleUrls: ['./yl2.component.less'],
  encapsulation: ViewEncapsulation.None
})

export class YL2OutModalComponent extends AppComponentBase {

  @ViewChild('secondModal') modal: ModalDirective;
  @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

  active = false;
  saving = false;

  chargeId = '';

  positionId = 0;

  quantity = 0;
  remark = '';

  pipeId = '';

  constructor(
    injector: Injector,
    private _outMaterialServiceProxy: MaterialOutServiceProxy
  ) {
    super(injector);
  }

  show(pipeId: string, chargeId: string, positionId: number): void {

    this.active = true;
    this.quantity = 0;
    this.remark = '';
    this.chargeId = chargeId;
    this.positionId = positionId;
    this.pipeId = pipeId;

    setTimeout(() => {
      this.modal.show();
    }, 0);
  }

  save(): void {

    if (!this.quantity) {
      this.notify.error('出库数量不能为0');
      return;
    }

    this.saving = true;

    this._outMaterialServiceProxy.outWarehouse(this.pipeId, this.chargeId, this.positionId, this.quantity, this.remark)
      .pipe(finalize(() => { this.saving = false; }))
      .subscribe(result => {
        this.close();
        this.modalSave.emit(null);
        this.notify.success('出库成功');
      });
  }

  close(): void {
    this.active = false;
    this.modal.hide();
  }

}
